package com.wanxi.education.controller.back;


import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.role.RolePageVo;
import com.wanxi.education.model.vo.role.RolePermissionVo;
import com.wanxi.education.model.vo.user.RoleVO;
import com.wanxi.education.service.RoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * @Author: Kang
 * @Date: 2021/9/28 14:07
 * @Description: 角色
 * @Version: 1.0
 */
@Api(tags={"角色相应API"})
@RestController
@RequestMapping("/back/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value="分页查询所有角色")
    @ApiParam(name = "rolePageVo", value = "分页参数", required = true)
    @PostMapping("/findPage")
    public CommonResult findPage(@RequestBody RolePageVo rolePageVo){
        List<RoleVO> roleVOList = roleService.findPage(rolePageVo.getPageNum(),rolePageVo.getPageSize());
        PageInfo<RoleVO> roleVOPageInfo = new PageInfo<>(roleVOList);
        return CommonResult.success(roleVOPageInfo);
    }

    @PostMapping("/add")
    @ApiOperation(value="新增角色", notes="添加角色,角色页面添加弹窗")
    @ApiParam(value = "新增角色参数", required = true)
    public CommonResult add(@RequestBody RoleVO roleVO){
        Boolean b = roleService.add(roleVO);
        return CommonResult.success(b);
    }


    @GetMapping("/find-by-role-id")
    @ApiParam(name = "roleId",value = "角色ID")
    @ApiOperation(value="通过角色Id查询角色信息")
    public CommonResult findByRoleId(@RequestParam(required = true) Long roleId){
        if (roleId == null) {
            CommonResult.validateFailed("参数不能为空");
        }
        RoleVO roleVO = roleService.findByRoleId(roleId);
        return CommonResult.success(roleVO);
    }

    @PostMapping("/update-by-id")
    @ApiOperation(value="超级管理修改角色")
    public CommonResult updateById(@ApiParam(value = "修改后的信息", required = true) @RequestBody RoleVO roleVO){
        Boolean b = roleService.updateById(roleVO);
        return CommonResult.success(b);
    }

    @GetMapping("/delete-by-role-id")
    @ApiParam(name = "roleId",value = "角色ID")
    @ApiOperation(value="通过角色ID删除角色信息")
    public CommonResult deleteByRoleId(@RequestParam(required = true) Long roleId){
        if (roleId == null) {
            CommonResult.validateFailed("参数不能为空");
        }
        Boolean b = roleService.deleteByRoleId(roleId);
        return CommonResult.success(b);
    }

    @ApiOperation("通过用户ID查询所属角色")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "userId",value = "需要查询的用户ID",dataType = "Long",paramType = "query")
    )
    @GetMapping("/find-by-user-id")
    public CommonResult findByUserId(Long userId) {
        Set<RoleVO> roleSet = roleService.findByUserId(userId);
        return CommonResult.success(roleSet);
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public CommonResult<List<RoleVO>> findAll() {
        List<RoleVO> list = roleService.findAll();
        return CommonResult.success(list);
    }

    @PostMapping("/updatePermissionsById")
    @ApiOperation(value = "超级管理修改角色的权限")
    public CommonResult updatePermissionsById(@ApiParam(value = "角色id", required = true) @RequestBody RolePermissionVo rolePermissionVo){
        roleService.updatePermissionsById(rolePermissionVo);
        return CommonResult.success("成功");
    }





}
